package jp.gr.java_conf.ionexux.feztimer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PowerFragment extends Fragment implements OnClickListener {

	public static String TAG = "PowerFragment";

	private Integer mActualPowerLapTime;
	private Integer mPowerCounter;
	private Handler mHandler = new Handler();
	private MediaPlayer mPlayer;
	private Timer mTimerPowerCounterView;
	private Timer mTimerPowerBackGround;
	private SharedPreferences mSharedPreferences;
	private SharedPreferences.Editor mEditor;
	private View mProgressBar;
	private View mButtonDown;
	private View mButtonUp;
	private View mButtonStart;
	private View mButtonStop;
	private View mTextViewPower;
	private Context mContext;
	private int ringTone;
	private boolean mPowerFlag = false;
	private int flashTime;

	@Override
	public void onAttach(Activity act) {
		super.onAttach(act);
		Log.d(TAG, "onAttach");
		mContext = getActivity();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		// 設定情報のインスタンス化
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(mContext);
		mEditor = mSharedPreferences.edit();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");

		// レイアウトを生成
		View v = inflater.inflate(R.layout.power, container, false);

		// 各種Viewを生成
		mButtonDown = v.findViewById(R.id.powerDownButton);
		mButtonUp = v.findViewById(R.id.powerUpButton);
		mButtonStart = v.findViewById(R.id.powerStartButton);
		mButtonStop = v.findViewById(R.id.powerStopButton);
		mTextViewPower = v.findViewById(R.id.powerTextView1);
		mProgressBar = v.findViewById(R.id.powerProgressBar1);

		// ボタンにクリックリスナーを設定する
		((Button) mButtonDown).setOnClickListener(this);
		((Button) mButtonUp).setOnClickListener(this);
		((Button) mButtonStart).setOnClickListener(this);
		((Button) mButtonStop).setOnClickListener(this);
		((TextView) mTextViewPower).setOnClickListener(this);

		return v;
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		super.onActivityCreated(bundle);
		Log.d(TAG, "onActivityCreated");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		mActualPowerLapTime = mSharedPreferences
				.getInt("powerLapTimeKey", 3000);
		mPowerCounter = mActualPowerLapTime;
		((TextView) mTextViewPower).setText(mPowerCounter.toString());

		ringTone = R.raw.powercounter;
		flashTime = 39;
		((ProgressBar) mProgressBar).setMax(mActualPowerLapTime);
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
		Log.d(TAG, mActualPowerLapTime.toString());
		// パワーカウンターのラップタイムの数値を保存する
		mEditor.putInt("powerLapTimeKey", mActualPowerLapTime);
		mEditor.commit();
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach");
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.powerDownButton:
			substractCounter();
			break;

		case R.id.powerUpButton:
			addCounter();
			break;

		case R.id.powerStartButton:
			startCounter();
			break;

		case R.id.powerStopButton:
			stopCounter();
			break;

		default:
			break;

		}
	}

	private void substractCounter() {
		if (mPowerFlag == true) {
			cancelCountTimer();
		}
		mActualPowerLapTime--;
		mPowerCounter = mActualPowerLapTime;
		((TextView) mTextViewPower).setText(mActualPowerLapTime.toString());
		((ProgressBar) mProgressBar).setMax(mActualPowerLapTime);
	}

	private void addCounter() {
		if (mPowerFlag == true) {
			cancelCountTimer();
		}
		mActualPowerLapTime++;
		mPowerCounter = mActualPowerLapTime;
		((TextView) mTextViewPower).setText(mActualPowerLapTime.toString());
		((ProgressBar) mProgressBar).setMax(mActualPowerLapTime);
	}

	private void startCounter() {
		mPowerCounter = mActualPowerLapTime;

		if (mPowerFlag)
			cancelCountTimer();

		mPowerFlag = true;

		mTimerPowerCounterView = new Timer();
		mTimerPowerCounterView.schedule(new TimerTask() {
			@Override
			public void run() {
				mHandler.post(new Runnable() {
					public void run() {
						mPowerCounter = mPowerCounter - flashTime;
						((TextView) mTextViewPower).setText(mPowerCounter
								.toString());
						((ProgressBar) mProgressBar)
								.setProgress(mActualPowerLapTime
										- mPowerCounter);
					}
				});
			}
		}, 0, flashTime);

		mTimerPowerBackGround = new Timer();
		mTimerPowerBackGround.schedule(new TimerTask() {

			@Override
			public void run() {
				mPowerCounter = mActualPowerLapTime;
				playSound(ringTone);
			}
		}, 0, mActualPowerLapTime);

	}

	private void stopCounter() {
		if (mPowerFlag) {
			cancelCountTimer();
			((TextView) mTextViewPower).setText(mActualPowerLapTime.toString());
			((ProgressBar) mProgressBar).setProgress(0);
		}
	}

	private void cancelCountTimer() {
		mPowerFlag = false;

		mTimerPowerBackGround.cancel();
		mTimerPowerBackGround = null;

		mTimerPowerCounterView.cancel();
		mTimerPowerCounterView = null;
	}

	public void playSound(int ringTone) {
		stopSound();
		mPlayer = MediaPlayer.create(mContext, ringTone);
		mPlayer.seekTo(0);
		mPlayer.start();
		mPlayer.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
				stopSound();
			}
		});

	}

	public void stopSound() {
		if (mPlayer == null)
			return;
		mPlayer.stop();
		mPlayer.setOnCompletionListener(null);
		mPlayer.release();
		mPlayer = null;
	}
}